package org.zjx.util;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class TextBlockSplitter {

    // 指定的字体颜色
    public static final Color[] TARGET_COLORS = {
            new Color(255, 255, 255),    // 白色
            new Color(255, 0, 0),    // 红色
            new Color(0, 255, 0),    // 绿色
            new Color(255, 255, 0)   // 黄色
    };

    // 颜色容差
    private static final int COLOR_TOLERANCE = 10;

    /**
     * 简单拆分文本块
     */
    public static List<Rectangle> splitTextBlocks(BufferedImage image) {
        List<Rectangle> blocks = new ArrayList<>();
        int width = image.getWidth();
        int height = image.getHeight();

        // 1. 记录每一行是否包含指定颜色
        List<Boolean> hasColorRows = new ArrayList<>();
        for (int y = 0; y < height; y++) {
            boolean hasColor = false;
            for (int x = 0; x < width/2; x++) {
                if (isTargetColor(new Color(image.getRGB(x, y)))) {
                    hasColor = true;
                    break;
                }
            }
            hasColorRows.add(hasColor);
        }

        // 2. 分段处理
        int startY = -1;
        int consecutiveEmptyRows = 0;

        for (int y = 0; y < height; y++) {
            if (hasColorRows.get(y)) {
                // 当前行有颜色
                if (startY == -1) {
                    // 新段落开始
                    startY = y;
                }
                consecutiveEmptyRows = 0;
            } else {
                // 当前行没有颜色
                consecutiveEmptyRows++;

                if (startY != -1 && consecutiveEmptyRows >= 13) {
                    // 达到13行空白，段落结束
                    int endY = y - consecutiveEmptyRows; // 回退到最后一个有颜色的行
                    Rectangle block = calculateBlockBounds(image, startY, endY);
                    if (block.width > 0 && block.height > 0) {
                        blocks.add(block);
                    }
                    startY = -1;
                    consecutiveEmptyRows = 0;
                }
            }
        }

        // 3. 处理最后一个段落（直接到图片底部）
        if (startY != -1) {
            Rectangle block = calculateBlockBounds(image, startY, height - 1);
            if (block.width > 0 && block.height > 0) {
                blocks.add(block);
            }
        }

        return blocks;
    }

    /**
     * 计算文本块的边界
     */
    private static Rectangle calculateBlockBounds(BufferedImage image, int startY, int endY) {
        int minX = image.getWidth();
        int maxX = 0;

        // 在段落范围内寻找左右边界
        for (int y = startY; y <= endY; y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                if (isTargetColor(new Color(image.getRGB(x, y)))) {
                    minX = Math.min(minX, x);
                    maxX = Math.max(maxX, x);
                }
            }
        }

        if (minX > maxX) {
            return new Rectangle(0, 0, 0, 0); // 无效区域
        }

        return new Rectangle(minX, startY, maxX - minX + 1, endY - startY + 1);
    }

    /**
     * 判断是否为指定颜色
     */
    private static boolean isTargetColor(Color color) {
        for (Color targetColor : TARGET_COLORS) {
            if (colorDistance(color, targetColor) <= COLOR_TOLERANCE) {
                return true;
            }
        }
        return false;
    }

    /**
     * 计算颜色距离
     */
    private static double colorDistance(Color c1, Color c2) {
        int rDiff = c1.getRed() - c2.getRed();
        int gDiff = c1.getGreen() - c2.getGreen();
        int bDiff = c1.getBlue() - c2.getBlue();
        return Math.sqrt(rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
    }
}